<?php
namespace app\admin\model;

use think\Model;

class ProductCategory extends Model
{
    public function getCreatorName()
    {
        return $this->getOperator()->bind(['create_name' => 'nickname']);
    }

    public function getUpdateName()
    {
        return $this->getUpdater()->bind(['update_name' => 'nickname']);
    }

    public function getOperator()
    {
        return $this->hasOne(Operator::class, 'id', 'creator');
    }

    public function getUpdater()
    {
        return $this->hasOne(Operator::class, 'id', 'updater');
    }

    public function getValueAttr($value, $data)
    {
        return $value . '';
    }
    public static function addParentName($result){
        $parentId = array_unique(array_column($result,'parent_id'));
        $parentCategory = self::where('id','in',$parentId)->select()->toArray();
        $parentCategory = array_column($parentCategory,null,'id');
        foreach ($result as $k=>$v){
            $result[$k]['parentName'] = $parentCategory[$v['parent_id']]['name']??'';
        }
        return $result;
    }
}

?>
